/* Class Map
 * 
 * 
 * 
 */
package disasterGameProj;

import java.util.Random;

public class Map
{
	// initial variables
	public Tile[][] tileList;
	public Random generator;
	public Tile[] buildingList;
	public Sprite spriteObj;
	public long money;
	public int timeRemaining;
	public String disasterType;
	private int waterStart;
	public Tile startTile;
	public DisasterQueue randomDisasters;
	
	// constructor
	Map( Sprite sprite, String disasterName )
	{
		waterStart = 0;
		startTile = null;
		disasterType = disasterName;
		timeRemaining = 600;
		money = 100000;
		spriteObj = sprite;
		tileList = new Tile[25][15];
		generator = new Random();
		randomDisasters = new DisasterQueue();
		
		for( int x = 0; x < 25; x++ )
			for( int y = 0; y < 15; y++ )
			{
				tileList[x][y] = new Tile();
				tileList[x][y].xLoc = x;
				tileList[x][y].yLoc = y;
			}
	}
	
	// creates the map that is displayed to the player
	public void createMap()
	{	
		int randomX = 0;
		int randomY = 0;
		int buildingIterator = 0;
		int randomBldTiles = generator.nextInt(80 - 70 ) + 70;
		buildingList = new Tile[ randomBldTiles];
		Tile[] tempBuildingList = null;
		Tile[] tempTileList = null;
		
		if( disasterType.equals( "tsunami" ) )
			setUpForTsunami();
			
		while ( randomBldTiles != 0 )
		{
			randomX = generator.nextInt(25);
			randomY = generator.nextInt(15);
			
			if( tileList[randomX][randomY].taken == false )
			{
				tileList[randomX][randomY].taken = true;
				tileList[randomX][randomY].img = spriteObj.buildingImg;
				tileList[randomX][randomY].myName = "B";
				tileList[randomX][randomY].partOfPath = true;
				buildingList[buildingIterator++] = tileList[randomX][randomY];	
				randomBldTiles -= 1;
				
			}// end if
			
		}// end while
		
		for( int i = 1; i < buildingList.length; i++ )
		{
			tempTileList = mapAStarAlgorithm( buildingList[0], buildingList[i] );
			
			if( tempTileList != null )
			{
				for( int k = 0; k < tempTileList.length; k++ )
				{
					tileList[tempTileList[k].xLoc][tempTileList[k].yLoc].myName = "R";
					tileList[tempTileList[k].xLoc][tempTileList[k].yLoc].partOfPath = true;
					tileList[tempTileList[k].xLoc][tempTileList[k].yLoc].taken = true;
				}
			}
			else
			{
				tileList[buildingList[i].xLoc][buildingList[i].yLoc].partOfPath = false;
				tileList[buildingList[i].xLoc][buildingList[i].yLoc].myName = "E";
			}
			
		}
		
		int curBldCount = countBuildings();
				
		int tempVal = 1;
		tempBuildingList = new Tile[ ( curBldCount + 1) ];
		tempBuildingList[0] = buildingList[0];
		for( int i = 1; i < buildingList.length; i++ )
			if( buildingList[i].myName.equals( "B" ) )
				tempBuildingList[tempVal++] = buildingList[i];
		
		buildingList = tempBuildingList;
		
		cleanUpMap();
		
		tileList[tempBuildingList[0].xLoc][tempBuildingList[0].yLoc].partOfPath = true;
		tileList[tempBuildingList[0].xLoc][tempBuildingList[0].yLoc].myName = "R";
		
		buildingList = new Tile[ curBldCount ];
		tempVal = 0;
		for( int i = 1; i < tempBuildingList.length; i++ )
			if( tempBuildingList[i].myName.equals( "B" ) )
				buildingList[tempVal++] = tempBuildingList[i];
		
		for( int y = 0; y < 15; y++ )
		{
			for( int x = 0; x < 25; x++ )
			{
				if( tileList[x][y].partOfPath == false )
				{
					tileList[x][y].taken = true;
					tileList[x][y].myName = "E";
					tileList[x][y].img = spriteObj.grassImg;
				}
			}
		}
		
		changeRoadImages();
		createBuildings();
		createRandomDisasters();
			
	}// end createMap
	
	// sets up the actual building objects within the tileList array
	private void createBuildings() 
	{
		int bldCount = countBuildings();
		int placedBuildings = 3;
		int randomNum = 0;
		
		while( placedBuildings != 0 )
		{
			randomNum = generator.nextInt( ( bldCount - 1 ) );
			
			if( buildingList[ randomNum ].buildingObj == null )
			{
				switch( placedBuildings )
				{
				case 1:
					buildingList[ randomNum ].buildingObj = new Building( "He" );
					buildingList[ randomNum ].img = spriteObj.buildingHelipad;
					placedBuildings--;
					break;
				case 2:
					buildingList[ randomNum ].buildingObj = new Building( "H" );
					buildingList[ randomNum ].img = spriteObj.buildingHospital;
					placedBuildings--;
					break;
				case 3:
					buildingList[ randomNum ].buildingObj = new Building( "F" );
					buildingList[ randomNum ].img = spriteObj.buildingFireStation;
					placedBuildings--;
					break;
				default:
					break;
				}
			}
		}
		
		for( int i = 0; i < bldCount; i++ )
			if( buildingList[ i ].buildingObj == null )
				buildingList[ i ].buildingObj = new Building( "B" );
	}

	// creates a boundary where the water will come from
	private void setUpForTsunami() 
	{
		waterStart = generator.nextInt( 4 - 1 ) + 1;
		
		switch( waterStart )
		{
		case 1:
			for( int x = 0; x < 25; x++ )
			{
				tileList[x][0].img = spriteObj.waterImg;
				tileList[x][0].taken = true;
				tileList[x][0].partOfPath = true;
			}
			break;
		case 2:
			for( int x = 0; x < 25; x++ )
			{
				tileList[x][14].img = spriteObj.waterImg;
				tileList[x][14].taken = true;
				tileList[x][14].partOfPath = true;
			}
			break;
		case 3:
			for( int y = 0; y < 15; y++ )
			{
				tileList[0][y].img = spriteObj.waterImg;
				tileList[0][y].taken = true;
				tileList[0][y].partOfPath = true;
			}
			break;
		case 4:
			for( int y = 0; y < 15; y++ )
			{
				tileList[24][y].img = spriteObj.waterImg;
				tileList[24][y].taken = true;
				tileList[24][y].partOfPath = true;
			}
			break;
		default:
			break;
		}
		
	}

	// algorithm chooses best path based upon heuristics
	// does not go through grass or buildings
	public Tile[] aStarAlgorithm( Tile startPos, Tile endPos )
	{
		boolean up = false;
		boolean left = false;
		boolean down = false;
		boolean right = false;
		boolean pathInOpenList = false;
		PathNode newPathNode = null;
		PathNode curPathNode = null;
		PathQueue openList = new PathQueue();
		PathQueue closedList = new PathQueue();
		
		openList.addPathNode( new PathNode( startPos ) );
		
		while( openList.isEmpty() != true )
		{
			
			curPathNode = openList.pullFromHeap();
			
			closedList.addPathNode( curPathNode );
			
			if( curPathNode.myTile == endPos )
				return completedPath( curPathNode );
			
			try
			{
			
				if( ( curPathNode.myTile.xLoc - 1 ) >= 0 )
				{
					if( tileList[(curPathNode.myTile.xLoc - 1)][curPathNode.myTile.yLoc].myName.equals("B") )
					{
						if( curPathNode.myTile.myName.equals( "R" ) )
							if( tileList[(curPathNode.myTile.xLoc - 1)][curPathNode.myTile.yLoc] == endPos )
								return completedPath( curPathNode );
					}
					else if ( tileList[(curPathNode.myTile.xLoc - 1)][curPathNode.myTile.yLoc].myName.equals( "E" ) )
					{}
					else if ( tileList[(curPathNode.myTile.xLoc - 1)][curPathNode.myTile.yLoc].myName.equals( "EMPTY" ) )
					{}
					else
					{	
						left = closedList.pathNodeExists( tileList[(curPathNode.myTile.xLoc - 1)][curPathNode.myTile.yLoc] );
						
						pathInOpenList = openList.pathNodeExists( tileList[(curPathNode.myTile.xLoc - 1)][curPathNode.myTile.yLoc] );
						
						if( pathInOpenList == false && left == false )
						{
							newPathNode = new PathNode( tileList[(curPathNode.myTile.xLoc - 1)][curPathNode.myTile.yLoc] );
							newPathNode.myParentNode = curPathNode;
							newPathNode.calculateHeuristics( curPathNode, endPos );
							openList.addPathNode( newPathNode );
						}
					}
				}
				
				if( ( curPathNode.myTile.xLoc + 1 ) < 25 )
				{
					if( tileList[(curPathNode.myTile.xLoc + 1)][curPathNode.myTile.yLoc].myName.equals("B") )
					{
						if( curPathNode.myTile.myName.equals( "R" ) )
							if( tileList[(curPathNode.myTile.xLoc + 1)][curPathNode.myTile.yLoc] == endPos )
								return completedPath( curPathNode );
					}
					else if ( tileList[(curPathNode.myTile.xLoc + 1)][curPathNode.myTile.yLoc].myName.equals( "E" ) )
					{}
					else if ( tileList[(curPathNode.myTile.xLoc + 1)][curPathNode.myTile.yLoc].myName.equals( "EMPTY" ) )
					{}
					else
					{
					
						right = closedList.pathNodeExists( tileList[(curPathNode.myTile.xLoc + 1)][curPathNode.myTile.yLoc] );
						
						pathInOpenList = openList.pathNodeExists( tileList[(curPathNode.myTile.xLoc + 1)][curPathNode.myTile.yLoc] );
						
						if( pathInOpenList == false && right == false )
						{
							newPathNode = new PathNode( tileList[(curPathNode.myTile.xLoc + 1)][curPathNode.myTile.yLoc] );
							newPathNode.myParentNode = curPathNode;
							newPathNode.calculateHeuristics( curPathNode, endPos );
							openList.addPathNode( newPathNode );
						}
					}
				}
				
				if( ( curPathNode.myTile.yLoc - 1 ) >= 0 )
				{
					if( tileList[curPathNode.myTile.xLoc][(curPathNode.myTile.yLoc - 1)].myName.equals("B") )
					{
						if( curPathNode.myTile.myName.equals( "R" ) )
							if( tileList[curPathNode.myTile.xLoc][(curPathNode.myTile.yLoc - 1)] == endPos )
								return completedPath( curPathNode );
					}
					else if ( tileList[curPathNode.myTile.xLoc][(curPathNode.myTile.yLoc - 1)].myName.equals( "E" ) )
					{}
					else if ( tileList[curPathNode.myTile.xLoc][(curPathNode.myTile.yLoc - 1)].myName.equals( "EMPTY" ) )
					{}
					else
					{
					
						up = closedList.pathNodeExists( tileList[curPathNode.myTile.xLoc][(curPathNode.myTile.yLoc - 1)] );
						
						pathInOpenList = openList.pathNodeExists( tileList[curPathNode.myTile.xLoc][(curPathNode.myTile.yLoc - 1)] );
						
						if( pathInOpenList == false && up == false )
						{
							newPathNode = new PathNode( tileList[curPathNode.myTile.xLoc][(curPathNode.myTile.yLoc - 1)] );
							newPathNode.myParentNode = curPathNode;
							newPathNode.calculateHeuristics( curPathNode, endPos );
							openList.addPathNode( newPathNode );
						}
					}
				}
				
				if( ( curPathNode.myTile.yLoc + 1 ) < 15 )
				{
					if( tileList[curPathNode.myTile.xLoc][(curPathNode.myTile.yLoc + 1)].myName.equals("B") )
					{
						if( curPathNode.myTile.myName.equals( "R" ) )
							if( tileList[curPathNode.myTile.xLoc][(curPathNode.myTile.yLoc + 1)] == endPos )
								return completedPath( curPathNode );
					}
					else if ( tileList[curPathNode.myTile.xLoc][(curPathNode.myTile.yLoc + 1)].myName.equals( "E" ) )
					{}
					else if ( tileList[curPathNode.myTile.xLoc][(curPathNode.myTile.yLoc + 1)].myName.equals( "EMPTY" ) )
					{}
					else
					{
	
						down = closedList.pathNodeExists( tileList[curPathNode.myTile.xLoc][(curPathNode.myTile.yLoc + 1)] );
						
						pathInOpenList = openList.pathNodeExists( tileList[curPathNode.myTile.xLoc][(curPathNode.myTile.yLoc + 1)] );
						
						if( pathInOpenList == false && down == false )
						{
							newPathNode = new PathNode( tileList[curPathNode.myTile.xLoc][(curPathNode.myTile.yLoc + 1)] );
							newPathNode.myParentNode = curPathNode;
							newPathNode.calculateHeuristics( curPathNode, endPos );
							openList.addPathNode( newPathNode );
						}
					}
				}
			}
			catch ( ArrayIndexOutOfBoundsException e  )
			{
				return null;
			}
			
			up = false;
			down = false;
			right = false;
			left = false;
		}
		
		return null;
	}// end method aStarAlgorithm
	
	// algorithm chooses best path based upon heuristics
	// does go through grass and buildings and empty tiles, used
	// in aid of creation of the map
	public Tile[] mapAStarAlgorithm( Tile startPos, Tile endPos )
	{
		boolean up = false;
		boolean left = false;
		boolean down = false;
		boolean right = false;
		boolean pathInOpenList = false;
		PathNode newPathNode = null;
		PathNode curPathNode = null;
		PathQueue openList = new PathQueue();
		PathQueue closedList = new PathQueue();
		
		openList.addPathNode( new PathNode( startPos ) );
		
		while( openList.isEmpty() != true )
		{
			
			curPathNode = openList.pullFromHeap();
			
			closedList.addPathNode( curPathNode );
			
			if( curPathNode.myTile == endPos )
				return completedPath( curPathNode );
			
			try
			{
			
				if( ( curPathNode.myTile.xLoc - 1 ) >= 0 )
				{
					if( tileList[(curPathNode.myTile.xLoc - 1)][curPathNode.myTile.yLoc].myName.equals("B") )
					{
						if( tileList[(curPathNode.myTile.xLoc - 1)][curPathNode.myTile.yLoc] == endPos )
							return completedPath( curPathNode );
					}
					else
					{	
						left = closedList.pathNodeExists( tileList[(curPathNode.myTile.xLoc - 1)][curPathNode.myTile.yLoc] );
						
						pathInOpenList = openList.pathNodeExists( tileList[(curPathNode.myTile.xLoc - 1)][curPathNode.myTile.yLoc] );
						
						if( pathInOpenList == false && left == false )
						{
							newPathNode = new PathNode( tileList[(curPathNode.myTile.xLoc - 1)][curPathNode.myTile.yLoc] );
							newPathNode.myParentNode = curPathNode;
							newPathNode.createMapHeuristics( curPathNode, endPos );
							openList.addPathNode( newPathNode );
						}
					}
				}
				
				if( ( curPathNode.myTile.xLoc + 1 ) < 25 )
				{
					if( tileList[(curPathNode.myTile.xLoc + 1)][curPathNode.myTile.yLoc].myName.equals("B") )
					{
						if( tileList[(curPathNode.myTile.xLoc + 1)][curPathNode.myTile.yLoc] == endPos )
							return completedPath( curPathNode );
					}
					else
					{
					
						right = closedList.pathNodeExists( tileList[(curPathNode.myTile.xLoc + 1)][curPathNode.myTile.yLoc] );
						
						pathInOpenList = openList.pathNodeExists( tileList[(curPathNode.myTile.xLoc + 1)][curPathNode.myTile.yLoc] );
						
						if( pathInOpenList == false && right == false )
						{
							newPathNode = new PathNode( tileList[(curPathNode.myTile.xLoc + 1)][curPathNode.myTile.yLoc] );
							newPathNode.myParentNode = curPathNode;
							newPathNode.createMapHeuristics( curPathNode, endPos );
							openList.addPathNode( newPathNode );
						}
					}
				}
				
				if( ( curPathNode.myTile.yLoc - 1 ) >= 0 )
				{
					if( tileList[curPathNode.myTile.xLoc][(curPathNode.myTile.yLoc - 1)].myName.equals("B") )
					{
						if( tileList[curPathNode.myTile.xLoc][(curPathNode.myTile.yLoc - 1)] == endPos )
							return completedPath( curPathNode );
					}
					else
					{
					
						up = closedList.pathNodeExists( tileList[curPathNode.myTile.xLoc][(curPathNode.myTile.yLoc - 1)] );
						
						pathInOpenList = openList.pathNodeExists( tileList[curPathNode.myTile.xLoc][(curPathNode.myTile.yLoc - 1)] );
						
						if( pathInOpenList == false && up == false )
						{
							newPathNode = new PathNode( tileList[curPathNode.myTile.xLoc][(curPathNode.myTile.yLoc - 1)] );
							newPathNode.myParentNode = curPathNode;
							newPathNode.createMapHeuristics( curPathNode, endPos );
							openList.addPathNode( newPathNode );
						}
					}
				}
				
				if( ( curPathNode.myTile.yLoc + 1 ) < 15 )
				{
					if( tileList[curPathNode.myTile.xLoc][(curPathNode.myTile.yLoc + 1)].myName.equals("B") )
					{
						if( tileList[curPathNode.myTile.xLoc][(curPathNode.myTile.yLoc + 1)] == endPos )
							return completedPath( curPathNode );
					}
					else
					{
	
						down = closedList.pathNodeExists( tileList[curPathNode.myTile.xLoc][(curPathNode.myTile.yLoc + 1)] );
						
						pathInOpenList = openList.pathNodeExists( tileList[curPathNode.myTile.xLoc][(curPathNode.myTile.yLoc + 1)] );
						
						if( pathInOpenList == false && down == false )
						{
							newPathNode = new PathNode( tileList[curPathNode.myTile.xLoc][(curPathNode.myTile.yLoc + 1)] );
							newPathNode.myParentNode = curPathNode;
							newPathNode.createMapHeuristics( curPathNode, endPos );
							openList.addPathNode( newPathNode );
						}
					}
				}
			}
			catch ( ArrayIndexOutOfBoundsException e  )
			{
				return null;
			}
			
			up = false;
			down = false;
			right = false;
			left = false;
		}
		
		return null;
	}// end method mapAStarAlgorithm
	
	public void changeRoadImages()
	{
		int up = 0;
		int down = 0;
		int left = 0;
		int right = 0;
		int total = 0;
		
		for( int y = 0; y < 15; y++ )
		{
			for( int x = 0; x < 25; x++)
			{
				if( tileList[x][y].myName.equals( "R" ) )
				{
					if( (x - 1) >= 0 )
					{
						if( tileList[(x-1)][y].myName.equals("R") )
							left = 42;
					}
					
					if( (x + 1) < 25 )
					{
						if( tileList[(x + 1)][y].myName.equals("R") )
							right = 31;
					}
					
					if( (y - 1) >= 0 )
					{
						if( tileList[x][(y - 1)].myName.equals("R") )
							up = 1;
					}
					
					if( (y + 1) < 15 )
					{
						if( tileList[x][(y + 1)].myName.equals("R") )
							down = 20;
					}
					
					total = right + left + up + down;
					
					switch ( total )
					{
					case 1:
						tileList[x][y].img = spriteObj.roadVertical;
						break;
					case 20:
						tileList[x][y].img = spriteObj.roadVertical;
						break;
					case 21:
						tileList[x][y].img = spriteObj.roadVertical;
						break;
					case 31:
						tileList[x][y].img = spriteObj.roadHorizontal;
						break;
					case 32:
						tileList[x][y].img = spriteObj.roadRightUp;
						break;
					case 42:
						tileList[x][y].img = spriteObj.roadHorizontal;
						break;
					case 43:
						tileList[x][y].img = spriteObj.roadLeftUp;
						break;
					case 51:
						tileList[x][y].img = spriteObj.roadRightDown;
						break;
					case 52:
						tileList[x][y].img = spriteObj.roadRightUpDown;
						break;
					case 62:
						tileList[x][y].img = spriteObj.roadLeftDown;
						break;
					case 63:
						tileList[x][y].img = spriteObj.roadLeftUpDown;
						break;
					case 73:
						tileList[x][y].img = spriteObj.roadHorizontal;
						break;
					case 74:
						tileList[x][y].img = spriteObj.roadLeftRightUp;
						break;
					case 93:
						tileList[x][y].img = spriteObj.roadLeftRightDown;
						break;
					case 94:
						tileList[x][y].img = spriteObj.roadCross;
						break;
					default:
						break;
					}
								
				}
				right = 0;
				left = 0;
				up = 0;
				down = 0;
				total = 0;
			}
		}
	}
	
	public int countBuildings()
	{
		int bldCount = 0;
		
		for( int i = 0; i < buildingList.length; i++ )
			if( buildingList[i].myName.equals( "B" ) )
				bldCount++;
		
		return bldCount;
	}
	
	public void setTentValues( int x, int y )
	{
		if ( ( x - 1 ) >= 0 )
		{
			if( tileList[ (x - 1) ][ y ].myName.equals( "B" ) )
				tileList[ (x - 1) ][ y ].buildingObj.tent = true;
			
			if( ( y + 1) < 15 )
				if( tileList[ (x - 1) ][ (y + 1) ].myName.equals( "B" ) )
					tileList[ (x - 1) ][ (y + 1) ].buildingObj.tent = true;	
		}
		
		if( ( x + 1 ) < 25 )
		{
			if( tileList[ (x + 1) ][ y ].myName.equals( "B" ) )
				tileList[ (x + 1) ][ y ].buildingObj.tent = true;
			
			if( ( y - 1) >= 0 )
				if( tileList[ (x + 1) ][ (y - 1) ].myName.equals( "B" ) )
					tileList[ (x + 1) ][ (y - 1) ].buildingObj.tent = true;	
		}
		
		if ( ( y - 1 ) >= 0 )
		{
			if( tileList[ x ][ (y - 1) ].myName.equals( "B" ) )
				tileList[ x ][ (y - 1) ].buildingObj.tent = true;
			
			if( ( x - 1) >=0 )
				if( tileList[ (x - 1) ][ (y - 1) ].myName.equals( "B" ) )
					tileList[ (x - 1) ][ (y - 1) ].buildingObj.tent = true;	
		}
		
		if( ( y + 1 ) < 15 )
		{
			if( tileList[ x ][ (y + 1) ].myName.equals( "B" ) )
				tileList[ x ][ (y + 1) ].buildingObj.tent = true;
			
			if( ( x + 1) < 25 )
				if( tileList[ (x + 1) ][ (y + 1) ].myName.equals( "B" ) )
					tileList[ (x + 1) ][ (y + 1) ].buildingObj.tent = true;	
		}
	}
	
	public void cleanUpMap()
	{
		boolean pathNeeded = false;
		Tile[] tempNode = null;
		
		for( int y = 0; y < 15; y++ )
		{
			for( int x = 0; x < 25; x++ )
			{
				if( tileList[x][y].myName.equals( "R" ) )
				{
					tileList[x][y].myName = "E";
					
					for( int i = 1; i < buildingList.length; i++ )
					{
						tempNode = aStarAlgorithm( buildingList[0], buildingList[i] );
						
						if( tempNode == null )
							pathNeeded = true;
					}
					
					if( pathNeeded == true )
						tileList[x][y].myName = "R";
					else
						tileList[x][y].partOfPath = false;
				}
				
				pathNeeded = false;
			}
		}
		
	}
	
	// used to return the completed path within the A Star Algorithm methods
	public Tile[] completedPath( PathNode curPathNode )
	{
		Tile[] tilePathList = null;
		
		if( curPathNode != null )
		{
			tilePathList = new Tile[ (curPathNode.g_Score + 1) ];
			
			for( int i = curPathNode.g_Score; i >= 0; i-- )
			{
				tilePathList[i] = curPathNode.myTile;
				curPathNode = curPathNode.myParentNode;
			}
			
			return tilePathList;
		}
		else 
			return null;
	}// end completedPath

	public void createRandomDisasters( )
	{
		DisasterNode temp = null;
		Random generator = new Random();
		int counter = 39;
		int bldNumber = 0;
		int priority = 0;
		int disasterType = 0;
		
		while ( counter != 0 )
		{
			disasterType = generator.nextInt( 2 - 1 ) + 1;
			bldNumber = generator.nextInt( buildingList.length );
			
			if( buildingList[ bldNumber ].buildingObj.myName.equals( "B" ) )
			{
				switch( disasterType )
				{
				case 1:// fire
					temp = new DisasterNode( buildingList[bldNumber].xLoc, buildingList[bldNumber].yLoc, priority, "fire" );
					randomDisasters.addDisasterNode( temp );
					counter--;
					priority++;
					break;
				case 2:// crumble
					temp = new DisasterNode( buildingList[bldNumber].xLoc, buildingList[bldNumber].yLoc, priority, "crumble" );
					randomDisasters.addDisasterNode( temp );
					counter--;
					priority++;
					break;
				default:
					break;
				}
			}
		}
		
	}
}